/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utilities;

import javax.swing.JFrame;
import prefuse.Constants;
import prefuse.Display;
import prefuse.Visualization;
import prefuse.action.ActionList;
import prefuse.action.RepaintAction;
import prefuse.action.assignment.ColorAction;
import prefuse.action.assignment.DataColorAction;
import prefuse.action.layout.graph.ForceDirectedLayout;
import prefuse.activity.Activity;
import prefuse.controls.DragControl;
import prefuse.controls.FocusControl;
import prefuse.controls.NeighborHighlightControl;
import prefuse.controls.PanControl;
import prefuse.controls.ZoomControl;
import prefuse.data.Graph;
import prefuse.data.Table;
import prefuse.data.io.sql.ConnectionFactory;
import prefuse.data.io.sql.DatabaseDataSource;
import prefuse.render.DefaultRendererFactory;
import prefuse.render.LabelRenderer;
import prefuse.util.ColorLib;
import prefuse.visual.VisualItem;

/**
 *
 * @author amendrashrestha
 */
public class TestMysql {

    public static final String driverName = "com.mysql.jdbc.Driver";
    public static final String dbURL = "jdbc:mysql://localhost:3306/test";
    public static final String userName = "root";
    public static final String userPwd = "black";

    public static void main(String[] args) {
        DatabaseDataSource datasrc = null;

        try {
            //get database connection
            datasrc = ConnectionFactory.getDatabaseConnection(driverName, dbURL, userName, userPwd);

            //create a table of data
            Table nodes = datasrc.getData("select * from node");
            Table edges = datasrc.getData("select * from edge");

            Graph graph = new Graph(nodes, edges, false, "Id", "Sid", "Tid");

            Visualization vis = new Visualization();
            vis.add("graph", graph);

            LabelRenderer r = new LabelRenderer("name");
            r.setRoundedCorner(8, 8);
            vis.setRendererFactory(new DefaultRendererFactory(r));

            int[] palette = new int[]{
                ColorLib.rgb(255, 180, 180), ColorLib.rgb(190, 190, 255)
            };

            DataColorAction fill = new DataColorAction("graph.nodes", "name",
                    Constants.NOMINAL, VisualItem.FILLCOLOR, palette);
            ColorAction textColor = new ColorAction("graph.nodes",
                    VisualItem.TEXTCOLOR, ColorLib.gray(0));
            ColorAction edgesColor = new ColorAction("graph.edges",
                    VisualItem.STROKECOLOR, ColorLib.gray(200));

            ActionList color = new ActionList();
            color.add(fill);
            color.add(textColor);
            color.add(edgesColor);

            ActionList layout = new ActionList(Activity.INFINITY);
            layout.add(new ForceDirectedLayout("graph"));
            layout.add(new RepaintAction());

            vis.putAction("color", color);
            vis.putAction("layout", layout);

            Display d = new Display(vis);
            d.pan(720/2, 500/2);
            d.setSize(720, 500);
            d.addControlListener(new FocusControl(1));
            d.addControlListener(new DragControl());
            d.addControlListener(new PanControl());
            d.addControlListener(new ZoomControl());
            d.addControlListener(new NeighborHighlightControl());

            JFrame frame = new JFrame("haha");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setSize(800, 600);
            frame.add(d);
            frame.setVisible(true);

            vis.run("color");
            vis.run("layout");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
